﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataBaseTable;
using MySpace.Data;

public partial class Manage_Member_Mode : System.Web.UI.Page
{
    protected string type = "0";
    private string memberid = "";
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            this.DropDownListbd();

        }
        if (!string.IsNullOrEmpty(Request.QueryString["memberid"]))
        {
            this.type = Request.QueryString["type"].ToString();
            this.memberid = Request.QueryString["memberid"].ToString();
            M_type.SelectedValue = this.type;
            PageTitle.Text = "更新成员信息";
            submit.Text = "更新成员";
            if (!IsPostBack)
            {
                this.BdData();
            }
            submit.Click += new EventHandler(update_member_Click);
        }
        else
        {
            PageTitle.Text = "添加社团成员";
            submit.Text = "添加成员";
            submit.Click += new EventHandler(add_member_Click);
        }
    }
    private void BdData()
    {
        MemberTable mt = new MemberTable(Convert.ToInt16(this.memberid));
        Name.Text = mt.Name;
        StuID.Text = mt.StuID;
        ZhiWu.Text = mt.ZhiWu;
        M_RX_Year.SelectedValue = mt.M_RX_Year+"年";
        M_RZ_Year.SelectedValue =string.IsNullOrEmpty(mt.M_RZ_Year)?"任职年份": mt.M_RZ_Year + "年";
        Phone.Text = mt.Phone;
        Short.Text = mt.Short;
        Address.Text = mt.Address;
        M_Class.Text = mt.M_Class;
    }
    private void DropDownListbd()
    {
        M_RX_Year.Items.Add("入学年份");
        M_RZ_Year.Items.Add("任职年份");
        for (int i = Convert.ToInt32(DateTime.Now.Year); i >= 1997; i--)
        {
            M_RX_Year.Items.Add(i.ToString() + "年");
            M_RZ_Year.Items.Add(i.ToString() + "年");
        }
    }

    protected void add_member_Click(object sender, EventArgs e)
    {
        try
        {
            MemberTable mt = new MemberTable();
            mt.M_Type = Convert.ToInt32(M_type.SelectedValue);
            mt.Name = Name.Text;
            mt.ZhiWu = ZhiWu.Text;
            mt.StuID = StuID.Text;
            mt.M_RZ_Year = M_RZ_Year.Text.Replace("任职年份","").Replace("年", "");
            mt.M_RX_Year = M_RX_Year.Text.Replace("年", "");
            mt.Phone = Phone.Text;
            mt.Short = Short.Text;
            mt.Address = Address.Text;
            mt.M_Class = M_Class.Text;
            mt.Insert();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加成员成功');window.location.href='Member_List.aspx?type=" + Convert.ToInt32(M_type.SelectedValue) + "';</script>");
        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('添加成员失败');</script>");
        }


    }
    protected void update_member_Click(object sender, EventArgs e)
    {
        MemberTable mt = new MemberTable(Convert.ToInt16(this.memberid));
        mt.M_Type = Convert.ToInt32(M_type.SelectedValue);
        mt.Name = Name.Text;
        mt.ZhiWu = ZhiWu.Text;
        mt.StuID = StuID.Text;
        mt.M_RZ_Year = M_RZ_Year.Text.Replace("任职年份", "").Replace("年", "");
        mt.M_RX_Year = M_RX_Year.Text.Replace("年", "");
        mt.Phone = Phone.Text;
        mt.Short = Short.Text;
        mt.Address = Address.Text;
        mt.M_Class = M_Class.Text;
        try
        {
            mt.Update();
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新成员成功');window.location.href='Member_List.aspx?type="+this.type+"';</script>");

        }
        catch
        {
            ClientScript.RegisterStartupScript(GetType(), "message", "<script>alert('更新成员失败');</script>");
        }
    }


}